package com.prs.crm.dao.ca.work.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.ca.work.PurchaseApplyDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.ca.work.PurchaseApply;

public class PurchaseApplyDaoImpl extends BaseDaoImpl<PurchaseApply> implements PurchaseApplyDao{

	@SuppressWarnings("unchecked")
	@Override
	public List<PurchaseApply> getAllPurchaseApply() {
		return (List<PurchaseApply>) this.getHibernateTemplate().execute(
		new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria=session.createCriteria(PurchaseApply.class);
				criteria.add(Restrictions.eq("pa_examineState", 1));//1：未审核 2：
				criteria.add(Restrictions.isNotNull("workFlow"));
				return criteria.list();
			}
		}
		);
	}

	@SuppressWarnings("unchecked")
	@Override
	public PurchaseApply getPurchaseApply(final String paBillNumber) {
		return (PurchaseApply) this.getHibernateTemplate().execute(
			new HibernateCallback() {
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					Criteria criteria=session.createCriteria(PurchaseApply.class);
					criteria.add(Restrictions.eq("paBillNumber", paBillNumber));
					List<PurchaseApply> purchase=criteria.list();
					if (purchase.size()==0) {
						return null;
					}else{
						return purchase.get(0);	
					}		
				}
			}
		);
	}

	@Override
	public void saveOrPurchaseApply(PurchaseApply purchaseApply) {
		this.getHibernateTemplate().saveOrUpdate(purchaseApply);
	}

}
